package com.matriq.cloudmanager.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.matriq.cloudmanager.MyApp;
import com.matriq.cloudmanager.entity.ClassCommunion;
import com.matriq.cloudmanager.entity.ClassContacts;
import com.matriq.cloudmanager.entity.ClassInfo;
import com.matriq.cloudmanager.entity.ComComment;
import com.matriq.cloudmanager.entity.CourseComment;
import com.matriq.cloudmanager.entity.CourseCommentSubject;
import com.matriq.cloudmanager.entity.FileData;
import com.matriq.cloudmanager.entity.Homework;
import com.matriq.cloudmanager.entity.LeaveDetail;
import com.matriq.cloudmanager.entity.LeaveMsg;
import com.matriq.cloudmanager.entity.Notice;
import com.matriq.cloudmanager.entity.PersonalAttendance;
import com.matriq.cloudmanager.entity.PersonalInfo;
import com.matriq.cloudmanager.entity.RemindNumber;
import com.matriq.cloudmanager.entity.StudyCommunion;
import com.matriq.cloudmanager.entity.TeachFiles;
import com.matriq.cloudmanager.entity.TeachPlan;
import com.matriq.cloudmanager.entity.Timtable;
import com.matriq.cloudmanager.entity.TypeComComment;
import com.matriq.cloudmanager.entity.TypeCourseComments;
import com.matriq.cloudmanager.entity.User;
import com.matriq.cloudmanager.jo.KeyValueJo;

import net.lgpage.http.HttpConnectionUtil;
import net.lgpage.http.HttpMsg;
import net.lgpage.http.ServiceUtil2;
import net.lgpage.tool.GsonTool;

public class AccountService {

	private static AccountService service;

	private AccountService() {
		super();
	}

	public static AccountService getService() {
		if (service == null)
			service = new AccountService();
		return service;
	}

	/**
	 * 登录接口
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public User login(String username, String password) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", username);
		map.put("password", password);
		return ServiceUtil2.getObjByMapFromHttpMsg("login", "POST", map, User.class);
	}

	public RemindNumber remind() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getObjByMapFromHttpMsg("remind", "POST", map, RemindNumber.class);
	}

	public List<Notice> notice() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("notice", "POST", map, Notice.class);
	}

	public HttpMsg noticecontent(String noticename) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("noticename", noticename);
		return ServiceUtil2.getHttpMsg("noticecontent", "POST", map);
	}

	public HttpMsg teach_purpose() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_purpose", "POST", map);
	}

	public HttpMsg teach_sys() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_sys", "POST", map);
	}

	public HttpMsg teach_policy() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_policy", "POST", map);
	}

	public HttpMsg teach_assessment() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_assessment", "POST", map);
	}

	public HttpMsg teach_ngo() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_ngo", "POST", map);
	}

	public HttpMsg teach_dissertation() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("teach_dissertation", "POST", map);
	}

	public List<Timtable> timtable() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("timtable", "POST", map, Timtable.class);
	}

	public TeachFiles material() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		HttpMsg msg = ServiceUtil2.getHttpMsg("material", "POST", map);
		JSONObject jo = new JSONObject(msg.getData().toString());
		TeachFiles obj = new TeachFiles();
		JSONArray ja = jo.getJSONArray("recommendFiles");
		List<FileData> recommendFiles = GsonTool.getList(ja, FileData.class);
		obj.setRecommendFiles(recommendFiles);

		ja = jo.getJSONArray("referenceFiles");
		List<FileData> referenceFiles = GsonTool.getList(ja, FileData.class);
		obj.setReferenceFiles(referenceFiles);

		ja = jo.getJSONArray("classFiles");
		List<FileData> classFiles = GsonTool.getList(ja, FileData.class);
		obj.setClassFiles(classFiles);
		return obj;
	}

	public TypeCourseComments course_comment() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		HttpMsg msg = ServiceUtil2.getHttpMsg("course_comment", "POST", map);
		JSONObject jo = new JSONObject(msg.getData().toString());
		TypeCourseComments obj = new TypeCourseComments();
		JSONArray ja = jo.getJSONArray("dispre");
		List<CourseComment> dispre = GsonTool.getList(ja, CourseComment.class);
		obj.setDispre(dispre);

		ja = jo.getJSONArray("disaft");
		List<CourseComment> disaft = GsonTool.getList(ja, CourseComment.class);
		obj.setDisaft(disaft);

		ja = jo.getJSONArray("disdone");
		List<CourseComment> disdone = GsonTool.getList(ja, CourseComment.class);
		obj.setDisdone(disdone);

		ja = jo.getJSONArray("NO");
		List<CourseComment> NO = GsonTool.getList(ja, CourseComment.class);
		obj.setNO(NO);
		return obj;
	}

	public List<CourseCommentSubject> course_comment_p(String courseid) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("courseid", courseid);
		return ServiceUtil2.getListByMapFromHttpMsg("course_comment_p", "POST", map, CourseCommentSubject.class);
	}
	
	public List<CourseCommentSubject> course_comment_c(String courseid) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("courseid", courseid);
		return ServiceUtil2.getListByMapFromHttpMsg("course_comment_c", "POST", map, CourseCommentSubject.class);
	}
	
	public HttpMsg comment_commit(String courseid,List<CourseCommentSubject>con) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("courseid", courseid);
		map.put("con", GsonTool.getGson().toJson(con));
		return ServiceUtil2.getHttpMsg("comment_commit", "POST", map);
	}
	
	public HttpMsg com_commit(List<ComComment>con) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("con", GsonTool.getGson().toJson(con));
		return ServiceUtil2.getHttpMsg("com_commit", "POST", map);
	}
	
	public TypeComComment com_comment() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		String res = HttpConnectionUtil.requestAppendMap("com_comment", "POST", map);
		HttpMsg msg = ServiceUtil2.checkHttpMsg(res);
		TypeComComment tmps = null;
		System.out.println(msg.getObj().toString());
		tmps = GsonTool.getGson().fromJson(msg.getObj().toString(), TypeComComment.class);
		if(tmps.getType()!=null&&tmps.getType() == 1){
			if("time_out".equals(msg.getMsg())){
				tmps.setType(3);
			}
		}
		return tmps;
	}
	
	public LeaveDetail ask_info(String id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("id", id);
		return ServiceUtil2.getObjByMapFromHttpMsg("ask_info", "POST", map,LeaveDetail.class);
	}
	public List<Homework> task() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("task", "POST", map, Homework.class);
	}
	
	public List<ClassCommunion> class_communion() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("class_communion", "POST", map, ClassCommunion.class);
	}
	public List<StudyCommunion> study_communion() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("study_communion", "POST", map, StudyCommunion.class);
	}
	public List<PersonalAttendance> check_personal() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("check_personal", "POST", map, PersonalAttendance.class);
	}
	public List<LeaveMsg> check_leave() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("check_leave", "POST", map, LeaveMsg.class);
	}
	public List<ClassInfo> base_class() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("base_class", "POST", map, ClassInfo.class);
	}
	
	public HttpMsg class_sub(String msg) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("msg", msg);
		return ServiceUtil2.getHttpMsg("class_sub", "POST", map);
	}
	public HttpMsg advise(String msg) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("msg", msg);
		return ServiceUtil2.getHttpMsg("advise", "POST", map);
	}
	
	public PersonalInfo base_personal() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getObjByMapFromHttpMsg("base_personal", "POST", map,PersonalInfo.class);
	}
	
	public List<ClassContacts> class_list() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("class_list", "POST", map, ClassContacts.class);
	}
	
	public HttpMsg classteacher_sub(String msg) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("msg", msg);
		return ServiceUtil2.getHttpMsg("classteacher_sub", "POST", map);
	}
	public HttpMsg info_mode(KeyValueJo val) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		map.put("val", GsonTool.getGson().toJson(val));
		return ServiceUtil2.getHttpMsg("info_mode", "POST", map);
	}
	
	public List<TeachPlan> teach_plan() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getListByMapFromHttpMsg("teach_plan", "POST", map, TeachPlan.class);
	}
	
	public HttpMsg school_manager() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("school_manager", "POST", map);
	}
	
	public HttpMsg school_info() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", MyApp.getApp().getUsername());
		return ServiceUtil2.getHttpMsg("school_info", "POST", map);
	}
	
}
